<template>
  <el-dialog :title="currentAsset.assetName" v-model="open" width="1000px" append-to-body>
    <!-- 资产基本信息 -->
    <div class="asset-info">
      <el-descriptions :column="4" border size="small">
        <el-descriptions-item label="资产编号">{{ currentAsset.assetNumber }}</el-descriptions-item>
        <el-descriptions-item label="资产名称">{{ currentAsset.assetName }}</el-descriptions-item>
        <el-descriptions-item label="规格型号">{{ currentAsset.specification }}</el-descriptions-item>
        <el-descriptions-item label="资产类别">{{ currentAsset.type }}</el-descriptions-item>
        <el-descriptions-item label="折旧方法">{{ currentAsset.depreciationMethod }}</el-descriptions-item>
        <el-descriptions-item label="残值率">{{ currentAsset.residualRate }}%</el-descriptions-item>
        <el-descriptions-item label="开始使用日期">{{ currentAsset.startUseDate }}</el-descriptions-item>
        <el-descriptions-item label="预计使用期间">{{ currentAsset.estimatedLife }}个月</el-descriptions-item>
      </el-descriptions>
    </div>

    <!-- 选项卡 -->
    <el-tabs v-model="activeTab" class="asset-tabs">
      <el-tab-pane label="原值变动" name="valueChange">
        <div class="change-content">
          <div class="comparison-container">
            <!-- 当前资产信息 -->
            <div class="current-info">
              <h3>当前资产信息</h3>
              <el-form label-width="100px" class="info-form">
                <el-form-item label="原值">
                  <span>{{ currentAsset.originalValue }}</span>
                </el-form-item>
                <el-form-item label="税额">
                  <span>{{ currentAsset.taxAmount }}</span>
                </el-form-item>
                <el-form-item label="原值(含税)">
                  <span>{{ currentAsset.originalValue + currentAsset.taxAmount }}</span>
                </el-form-item>
                <el-form-item label="累计折旧">
                  <span>{{ currentAsset.accumDepreciation }}</span>
                </el-form-item>
                <el-form-item label="资产净值">
                  <span>{{ currentAsset.endingNetValue }}</span>
                </el-form-item>
                <el-form-item label="月折旧额">
                  <span>{{ currentAsset.monthlyDepreciation }}</span>
                </el-form-item>
              </el-form>
            </div>

            <!-- 变更内容 -->
            <div class="change-info">
              <h3>变更内容</h3>
              <el-form ref="changeFormRef" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="原值" prop="originalValue">
                  <el-input-number v-model="form.originalValue" :precision="2" :step="100" />
                </el-form-item>
                <el-form-item label="税额" prop="taxAmount">
                  <el-input-number v-model="form.taxAmount" :precision="2" :step="10" />
                </el-form-item>
                <el-form-item label="原值(含税)">
                  <el-input-number v-model="form.originalValueWithTax" :precision="2" disabled />
                </el-form-item>
                <el-form-item label="累计折旧" prop="accumDepreciation">
                  <el-input-number v-model="form.accumDepreciation" :precision="2" :step="100" />
                </el-form-item>
                <el-form-item label="资产净值">
                  <el-input-number v-model="form.endingNetValue" :precision="2" disabled />
                </el-form-item>
                <el-form-item label="月折旧额" prop="monthlyDepreciation">
                  <span>{{ form.monthlyDepreciation }}</span>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="使用年限调整" name="lifeAdjust">
        <div class="change-content">
          <div class="comparison-container">
            <!-- 当前资产信息 -->
            <div class="current-info">
              <h3>当前资产信息</h3>
              <el-form label-width="120px" class="info-form">
                <el-form-item label="折旧方法">
                  <span>{{ currentAsset.depreciationMethod }}</span>
                </el-form-item>
                <el-form-item label="原值(含税)">
                  <span>{{ (props.currentAsset.originalValue || 0) + (props.currentAsset.taxAmount || 0) }}</span>
                </el-form-item>
                <el-form-item label="累计折旧">
                  <span>{{ currentAsset.accumDepreciation }}</span>
                </el-form-item>
                <el-form-item label="残值率">
                  <span>{{ currentAsset.residualRate }}%</span>
                </el-form-item>
                <el-form-item label="预计使用期间">
                  <span>{{ currentAsset.estimatedLife }}</span>
                </el-form-item>
                <el-form-item label="月折旧额">
                  <span>{{ currentAsset.monthlyDepreciation }}</span>
                </el-form-item>
              </el-form>
            </div>

            <!-- 变更内容 -->
            <div class="change-info">
              <h3>变更内容</h3>
              <el-form ref="changeFormRef" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="折旧方法" prop="depreciationMethod">
                  <el-select v-model="form.depreciationMethod" placeholder="请选择折旧方法">
                    <el-option label="年限平均法" value="年限平均法" />
                    <el-option label="工作量法" value="工作量法" />
                    <el-option label="双倍余额递减法" value="双倍余额递减法" />
                  </el-select>
                </el-form-item>
                <el-form-item label="原值(含税)">
                  <span>{{ (props.currentAsset.originalValue || 0) + (props.currentAsset.taxAmount || 0) }}</span>
                </el-form-item>
                <el-form-item label="累计折旧">
                  <span>{{ currentAsset.accumDepreciation }}</span>
                </el-form-item>
                <el-form-item label="残值率" prop="residualRate">
                  <el-input-number v-model="form.residualRate" :precision="1" :step="0.1" :min="0" :max="100">
                    <template #suffix>%</template>
                  </el-input-number>
                </el-form-item>
                <el-form-item label="预计使用期间" prop="estimatedLife">
                  <el-input-number v-model="form.estimatedLife" :min="1" :step="1" />
                  <span class="unit">期</span>
                </el-form-item>
                <el-form-item label="月折旧额">
                  <span>{{ calculatedMethodMonthlyDepreciation }}</span>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="累计折旧调整" name="accumDepreciationAdjust">
        <div class="change-content">
          <div class="comparison-container">
            <!-- 当前资产信息 -->
            <div class="current-info">
              <h3>当前资产信息</h3>
              <el-form label-width="120px" class="info-form">
                <el-form-item label="原值">
                  <span>{{ currentAsset.originalValue }}</span>
                </el-form-item>
                <el-form-item label="税额">
                  <span>{{ currentAsset.taxAmount }}</span>
                </el-form-item>
                <el-form-item label="原值(含税)">
                  <span>{{ (props.currentAsset.originalValue || 0) + (props.currentAsset.taxAmount || 0) }}</span>
                </el-form-item>
                <el-form-item label="累计折旧">
                  <span>{{ currentAsset.accumDepreciation }}</span>
                </el-form-item>
                <el-form-item label="资产净值">
                  <span>{{ currentAsset.endingNetValue }}</span>
                </el-form-item>
                <el-form-item label="月折旧额">
                  <span>{{ currentAsset.monthlyDepreciation }}</span>
                </el-form-item>
              </el-form>
            </div>

            <!-- 变更内容 -->
            <div class="change-info">
              <h3>变更内容</h3>
              <el-form ref="changeFormRef" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="原值">
                  <span>{{ currentAsset.originalValue }}</span>
                </el-form-item>
                <el-form-item label="税额">
                  <span>{{ currentAsset.taxAmount }}</span>
                </el-form-item>
                <el-form-item label="原值(含税)">
                  <span>{{ (props.currentAsset.originalValue || 0) + (props.currentAsset.taxAmount || 0) }}</span>
                </el-form-item>
                <el-form-item label="累计折旧" prop="accumDepreciation">
                  <el-input-number v-model="form.accumDepreciation" :precision="2" :step="100" />
                </el-form-item>
                <el-form-item label="资产净值">
                  <span>{{ calculatedAccumNetValue }}</span>
                </el-form-item>
                <el-form-item label="月折旧额">
                  <span>{{ currentAsset.monthlyDepreciation }}</span>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="折旧方式调整" name="methodAdjust">
        <div class="change-content">
          <div class="comparison-container">
            <!-- 当前资产信息 -->
            <div class="current-info">
              <h3>当前资产信息</h3>
              <el-form label-width="120px" class="info-form">
                <el-form-item label="折旧方法">
                  <span>{{ currentAsset.depreciationMethod }}</span>
                </el-form-item>
                <el-form-item label="原值(含税)">
                  <span>{{ (props.currentAsset.originalValue || 0) + (props.currentAsset.taxAmount || 0) }}</span>
                </el-form-item>
                <el-form-item label="累计折旧">
                  <span>{{ currentAsset.accumDepreciation }}</span>
                </el-form-item>
                <el-form-item label="残值率">
                  <span>{{ currentAsset.residualRate }}%</span>
                </el-form-item>
                <el-form-item label="预计使用期间">
                  <span>{{ currentAsset.estimatedLife }}</span>
                </el-form-item>
                <el-form-item label="月折旧额">
                  <span>{{ currentAsset.monthlyDepreciation }}</span>
                </el-form-item>
              </el-form>
            </div>

            <!-- 变更内容 -->
            <div class="change-info">
              <h3>变更内容</h3>
              <el-form ref="changeFormRef" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="折旧方法" prop="depreciationMethod">
                  <el-select v-model="form.depreciationMethod" placeholder="请选择折旧方法">
                    <el-option label="年限平均法" value="年限平均法" />
                    <el-option label="工作量法" value="工作量法" />
                    <el-option label="双倍余额递减法" value="双倍余额递减法" />
                  </el-select>
                </el-form-item>
                <el-form-item label="原值(含税)">
                  <span>{{ (props.currentAsset.originalValue || 0) + (props.currentAsset.taxAmount || 0) }}</span>
                </el-form-item>
                <el-form-item label="累计折旧">
                  <span>{{ currentAsset.accumDepreciation }}</span>
                </el-form-item>
                <el-form-item label="残值率" prop="residualRate">
                  <el-input-number v-model="form.residualRate" :precision="1" :step="0.1" :min="0" :max="100">
                    <template #suffix>%</template>
                  </el-input-number>
                </el-form-item>
                <el-form-item label="预计使用期间" prop="estimatedLife">
                  <el-input-number v-model="form.estimatedLife" :min="1" :step="1" />
                  <span class="unit">期</span>
                </el-form-item>
                <el-form-item label="月折旧额">
                  <span>{{ calculatedMethodMonthlyDepreciation }}</span>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="残值调整" name="residualAdjust">
        <div class="change-content">
          <div class="comparison-container">
            <!-- 当前资产信息 -->
            <div class="current-info">
              <h3>当前资产信息</h3>
              <el-form label-width="120px" class="info-form">
                <el-form-item label="折旧方法">
                  <span>{{ currentAsset.depreciationMethod }}</span>
                </el-form-item>
                <el-form-item label="原值(含税)">
                  <span>{{ (props.currentAsset.originalValue || 0) + (props.currentAsset.taxAmount || 0) }}</span>
                </el-form-item>
                <el-form-item label="累计折旧">
                  <span>{{ currentAsset.accumDepreciation }}</span>
                </el-form-item>
                <el-form-item label="残值率">
                  <span>{{ currentAsset.residualRate }}%</span>
                </el-form-item>
                <el-form-item label="预计使用期间">
                  <span>{{ currentAsset.estimatedLife }}</span>
                </el-form-item>
                <el-form-item label="月折旧额">
                  <span>{{ currentAsset.monthlyDepreciation }}</span>
                </el-form-item>
              </el-form>
            </div>

            <!-- 变更内容 -->
            <div class="change-info">
              <h3>变更内容</h3>
              <el-form ref="changeFormRef" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="折旧方法">
                  <span>{{ currentAsset.depreciationMethod }}</span>
                </el-form-item>
                <el-form-item label="原值(含税)">
                  <span>{{ (props.currentAsset.originalValue || 0) + (props.currentAsset.taxAmount || 0) }}</span>
                </el-form-item>
                <el-form-item label="累计折旧">
                  <span>{{ currentAsset.accumDepreciation }}</span>
                </el-form-item>
                <el-form-item label="残值率" prop="residualRate">
                  <el-input-number v-model="form.residualRate" :precision="1" :step="0.1" :min="0" :max="100">
                    <template #suffix>%</template>
                  </el-input-number>
                </el-form-item>
                <el-form-item label="预计使用期间">
                  <span>{{ currentAsset.estimatedLife }}</span>
                </el-form-item>
                <el-form-item label="月折旧额">
                  <span>{{ calculatedResidualMonthlyDepreciation }}</span>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="使用部门调整" name="deptAdjust">
        <div class="change-content">
          <div class="comparison-container">
            <!-- 当前资产信息 -->
            <div class="current-info">
              <h3>当前资产信息</h3>
              <el-form label-width="120px" class="info-form">
                <el-form-item label="使用部门">
                  <span>{{ currentAsset.useDepartment }}</span>
                </el-form-item>
                <el-form-item label="使用人">
                  <span>{{ currentAsset.user }}</span>
                </el-form-item>
                <el-form-item label="备注">
                  <span>{{ currentAsset.remark || '无' }}</span>
                </el-form-item>
              </el-form>
            </div>

            <!-- 变更内容 -->
            <div class="change-info">
              <h3>变更内容</h3>
              <el-form ref="changeFormRef" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="使用部门" prop="useDepartment">
                  <el-select v-model="form.useDepartment" placeholder="请选择使用部门">
                    <el-option label="财务部" value="001" />
                    <el-option label="IT部" value="002" />
                    <el-option label="行政部" value="003" />
                  </el-select>
                </el-form-item>
                <el-form-item label="使用人" prop="user">
                  <el-input v-model="form.user" placeholder="请输入使用人" />
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="科目调整" name="accountAdjust">
        <div class="change-content">
          <div class="comparison-container">
            <!-- 当前资产信息 -->
            <div class="current-info">
              <h3>当前资产信息</h3>
              <el-form label-width="120px" class="info-form">
                <el-form-item label="固定资产科目">
                  <span>{{ currentAsset.fixedAssetAccount  }}</span>
                </el-form-item>
                <el-form-item label="减值准备科目">
                  <span>{{ currentAsset.impairmentAccount }}</span>
                </el-form-item>
                <el-form-item label="累计折旧科目">
                  <span>{{ currentAsset.accumDepreciationAccount  }}</span>
                </el-form-item>
                <el-form-item label="折旧费用科目">
                  <span>{{ currentAsset.depreciationExpenseAccount  }}</span>
                </el-form-item>
                <el-form-item label="税金科目">
                  <span>{{ currentAsset.taxAccount }}</span>
                </el-form-item>
                <el-form-item label="资产清理科目">
                  <span>{{ currentAsset.assetDisposalAccount }}</span>
                </el-form-item>
                <el-form-item label="资产购入对方科目">
                  <span>{{ currentAsset.purchaseAccount }}</span>
                </el-form-item>
              </el-form>
            </div>

            <!-- 变更内容 -->
            <div class="change-info">
              <h3>变更内容</h3>
              <el-form ref="changeFormRef" :model="form" :rules="rules" label-width="120px">
                <el-form-item label="减值准备科目" prop="impairmentAccount">
                  <el-select v-model="form.impairmentAccount" placeholder="请选择减值准备科目">
                    <el-option label="1601 固定资产减值准备" value="1601 固定资产减值准备" />
                    <el-option label="1602 在建工程减值准备" value="1602 在建工程减值准备" />
                    <el-option label="1603 工程物资减值准备" value="1603 工程物资减值准备" />
                  </el-select>
                </el-form-item>
                <el-form-item label="固定资产科目" prop="fixedAssetAccount">
                  <el-select v-model="form.fixedAssetAccount" placeholder="请选择固定资产科目">
                    <el-option label="1601 房屋及建筑物" value="1601 房屋及建筑物" />
                    <el-option label="1602 机器设备" value="1602 机器设备" />
                    <el-option label="1603 运输工具" value="1603 运输工具" />
                    <el-option label="1604 电子设备" value="1604 电子设备" />
                  </el-select>
                </el-form-item>
                <el-form-item label="累计折旧科目" prop="accumDepreciationAccount">
                  <el-select v-model="form.accumDepreciationAccount" placeholder="请选择累计折旧科目">
                    <el-option label="1602001 房屋建筑物累计折旧" value="1602001 房屋建筑物累计折旧" />
                    <el-option label="1602002 机器设备累计折旧" value="1602002 机器设备累计折旧" />
                    <el-option label="1602003 运输工具累计折旧" value="1602003 运输工具累计折旧" />
                  </el-select>
                </el-form-item>
                <el-form-item label="折旧费用科目" prop="depreciationExpenseAccount">
                  <el-select v-model="form.depreciationExpenseAccount" placeholder="请选择折旧费用科目">
                    <el-option label="5601 管理费用-折旧费" value="5601 管理费用-折旧费" />
                    <el-option label="5602 制造费用-折旧费" value="5602 制造费用-折旧费" />
                    <el-option label="5603 销售费用-折旧费" value="5603 销售费用-折旧费" />
                  </el-select>
                </el-form-item>
                <el-form-item label="税金科目" prop="taxAccount">
                  <el-select v-model="form.taxAccount" placeholder="请选择税金科目">
                    <el-option label="22210101 应交增值税-进项税额" value="22210101 应交增值税-进项税额" />
                    <el-option label="22210102 应交增值税-销项税额" value="22210102 应交增值税-销项税额" />
                    <el-option label="22210103 应交增值税-未交增值税" value="22210103 应交增值税-未交增值税" />
                  </el-select>
                </el-form-item>
                <el-form-item label="资产清理科目" prop="assetDisposalAccount">
                  <el-select v-model="form.assetDisposalAccount" placeholder="请选择资产清理科目">
                    <el-option label="1606001 固定资产清理-房屋建筑物" value="1606001 固定资产清理-房屋建筑物" />
                    <el-option label="1606002 固定资产清理-机器设备" value="1606002 固定资产清理-机器设备" />
                    <el-option label="1606003 固定资产清理-运输工具" value="1606003 固定资产清理-运输工具" />
                  </el-select>
                </el-form-item>
                <el-form-item label="资产购入对方科目" prop="purchaseAccount">
                  <el-select v-model="form.purchaseAccount" placeholder="请选择资产购入对方科目">
                    <el-option label="1002 银行存款" value="1002 银行存款" />
                    <el-option label="2202 应付账款" value="2202 应付账款" />
                    <el-option label="2203 应付票据" value="2203 应付票据" />
                  </el-select>
                </el-form-item>
              </el-form>
            </div>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>

    <!-- 公共备注字段 -->
    <div class="common-remark" style="margin-top: 20px;">
      <el-form ref="remarkFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入变动原因及说明"
            :rows="3"
          />
        </el-form-item>
      </el-form>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, reactive, computed, watch, nextTick } from 'vue'
import { ElMessage } from 'element-plus'

const props = defineProps({
  currentAsset: {
    type: Object,
    required: true
  }
})

const emit = defineEmits(['update:modelValue', 'success'])

const open = ref(false)
const activeTab = ref('valueChange')
const changeFormRef = ref()
const lifeFormRef = ref()
const methodFormRef = ref()
const accumDepreciationFormRef = ref()
const residualFormRef = ref()
const deptFormRef = ref()
const accountFormRef = ref()

const data = reactive({
  // 部门编码映射
  departmentMap: {
    '001': '财务部',
    '002': 'IT部',
    '003': '行政部'
  },
  // 部门编码映射的反向映射
  departmentCodeMap: {
    '财务部': '001',
    'IT部': '002',
    '行政部': '003'
  },
  form: {
    // 基础信息
    id: null,
    assetNumber: '',
    assetName: '',
    specification: '',
    originalValue: 0,
    taxAmount: 0,
    originalValueWithTax: 0,
    accumDepreciation: 0,
    netValue: 0,
    monthlyDepreciation: 0,
    depreciationMethod: '',
    estimatedLife: 0,
    usedYear: '',
    assetCategory: '',
    purchaseDate: '',
    entryPeriod: '',
    startUseDate: '',
    location: '',
    useDepartment: '',
    user: '',
    supplier: '',
    addType: '',
    copyCount: 0,
    depreciateNow: 0,
    residualRate: 0,
    changeDate: '',
    remark: '',
    // 科目信息
    impairmentAccount: '',
    fixedAssetAccount: '',
    accumDepreciationAccount: '',
    depreciationExpenseAccount: '',
    taxAccount: '',
    assetDisposalAccount: '',
    purchaseAccount: '',
    department:"",
     type:''
  },
  lifeForm: {
    estimatedLife: 0,
    changeDate: '',
    remark: ''
  },
  methodForm: {
    depreciationMethod: '',
    residualRate: 0,
    estimatedLife: 0,
    changeDate: '',
    remark: ''
  },
  accumDepreciationForm: {
    accumDepreciation: 0,
    changeDate: '',
    remark: ''
  },
  residualForm: {
    residualRate: 0,
    changeDate: '',
    remark: ''
  },
  deptForm: {
    useDepartment: '',
    user: '',
    changeDate: '',
    remark: ''
  },
  accountForm: {
    fixedAssetAccount: '',
    depreciationAccount: '',
    accumulatedDepreciationAccount: '',
    depreciationExpenseAccount: '',
    taxAccount: '',
    assetClearAccount: '',
    assetPurchaseAccount: '',
    changeDate: '',
    remark: ''
  },
  rules: {
    // 基础信息验证规则
    assetNumber: [{ required: true, message: '请输入资产编号', trigger: 'blur' }],
    assetName: [{ required: true, message: '请输入资产名称', trigger: 'blur' }],
    originalValue: [{ required: true, message: '请输入原值', trigger: 'blur' }],
    taxAmount: [{ required: true, message: '请输入税额', trigger: 'blur' }],
    accumDepreciation: [{ required: true, message: '请输入累计折旧', trigger: 'blur' }],
    depreciationMethod: [{ required: true, message: '请选择折旧方法', trigger: 'change' }],
    estimatedLife: [{ required: true, message: '请输入预计使用期间', trigger: 'blur' }],
    residualRate: [{ required: true, message: '请输入残值率', trigger: 'blur' }],
    useDepartment: [{ required: true, message: '请选择使用部门', trigger: 'change' }],
    user: [{ required: true, message: '请输入使用人', trigger: 'blur' }],
    changeDate: [{ required: true, message: '请选择变动期间', trigger: 'blur' }],
    // 科目验证规则
    impairmentAccount: [{ required: true, message: '请选择减值准备科目', trigger: 'change' }],
    fixedAssetAccount: [{ required: true, message: '请选择固定资产科目', trigger: 'change' }],
    accumDepreciationAccount: [{ required: true, message: '请选择累计折旧科目', trigger: 'change' }],
    depreciationExpenseAccount: [{ required: true, message: '请选择折旧费用科目', trigger: 'change' }],
    taxAccount: [{ required: true, message: '请选择税金科目', trigger: 'change' }],
    assetDisposalAccount: [{ required: true, message: '请选择资产清理科目', trigger: 'change' }],
    purchaseAccount: [{ required: true, message: '请选择资产购入对方科目', trigger: 'change' }]
  },
  lifeRules: {
    estimatedLife: [
      { required: true, message: '请输入预计使用期间', trigger: 'blur' }
    ],
    changeDate: [
      { required: true, message: '请选择变动期间', trigger: 'blur' }
    ]
  },
  methodRules: {
    depreciationMethod: [
      { required: true, message: '请选择折旧方法', trigger: 'change' }
    ],
    residualRate: [
      { required: true, message: '请输入残值率', trigger: 'blur' }
    ],
    estimatedLife: [
      { required: true, message: '请输入预计使用期间', trigger: 'blur' }
    ],
    changeDate: [
      { required: true, message: '请选择变动期间', trigger: 'blur' }
    ]
  },
  accumDepreciationRules: {
    accumDepreciation: [
      { required: true, message: '请输入累计折旧', trigger: 'blur' }
    ],
    changeDate: [
      { required: true, message: '请选择变动期间', trigger: 'blur' }
    ]
  },
  residualRules: {
    residualRate: [
      { required: true, message: '请输入残值率', trigger: 'blur' }
    ],
    changeDate: [
      { required: true, message: '请选择变动期间', trigger: 'blur' }
    ]
  },
  deptRules: {
    useDepartment: [
      { required: true, message: '请选择使用部门', trigger: 'change' }
    ],
    user: [
      { required: true, message: '请输入使用人', trigger: 'blur' }
    ],
    changeDate: [
      { required: true, message: '请选择变动期间', trigger: 'blur' }
    ]
  },
  accountRules: {
    fixedAssetAccount: [
      { required: true, message: '请选择固定资产科目', trigger: 'change' }
    ],
    depreciationAccount: [
      { required: true, message: '请选择减值准备科目', trigger: 'change' }
    ],
    accumulatedDepreciationAccount: [
      { required: true, message: '请选择累计折旧科目', trigger: 'change' }
    ],
    depreciationExpenseAccount: [
      { required: true, message: '请选择折旧费用科目', trigger: 'change' }
    ],
    taxAccount: [
      { required: true, message: '请选择税金科目', trigger: 'change' }
    ],
    assetClearAccount: [
      { required: true, message: '请选择资产清理科目', trigger: 'change' }
    ],
    assetPurchaseAccount: [
      { required: true, message: '请选择资产购入对方科目', trigger: 'change' }
    ],
    changeDate: [
      { required: true, message: '请选择变动期间', trigger: 'blur' }
    ]
  }
})

const { form, lifeForm, methodForm, accumDepreciationForm, rules, lifeRules, methodRules, accumDepreciationRules, residualForm, residualRules, deptForm, accountForm } = toRefs(data)

// 计算月折旧额
const monthlyDepreciation = computed(() => {
  if (!form.value.estimatedLife || !props.currentAsset) return 0
  const depreciableAmount = (props.currentAsset.originalValue || 0) * (1 - (props.currentAsset.residualRate || 0) / 100)
  return (depreciableAmount / form.value.estimatedLife).toFixed(2)
})

// 计算折旧方式调整的月折旧额
const calculatedMethodMonthlyDepreciation = computed(() => {
  if (!form.value.estimatedLife || !props.currentAsset) return 0
  const depreciableAmount = (props.currentAsset.originalValue || 0) * (1 - (form.value.residualRate || 0) / 100)
  return (depreciableAmount / form.value.estimatedLife).toFixed(2)
})

// 计算累计折旧调整后的资产净值
const calculatedAccumNetValue = computed(() => {
  if (!props.currentAsset) return 0
  const originalValueWithTax = (props.currentAsset.originalValue || 0) + (props.currentAsset.taxAmount || 0)
  return (originalValueWithTax - (accumDepreciationForm.value.accumDepreciation || 0)).toFixed(2)
})

// 计算残值调整的月折旧额
const calculatedResidualMonthlyDepreciation = computed(() => {
  if (!props.currentAsset) return 0
  const depreciableAmount = (props.currentAsset.originalValue || 0) * (1 - (form.value.residualRate || 0) / 100)
  return (depreciableAmount / (props.currentAsset.estimatedLife || 1)).toFixed(2)
})

// 监听原值和税额的变化，自动计算含税原值
watch([() => form.value.originalValue, () => form.value.taxAmount], 
  ([newOriginal, newTax]) => {
    form.value.originalValueWithTax = Number(newOriginal || 0) + Number(newTax || 0)
    // 计算资产净值
    form.value.endingNetValue = form.value.originalValueWithTax - form.value.accumDepreciation
  }
)

// 监听累计折旧的变化，自动计算资产净值
watch(() => form.value.accumDepreciation, 
  (newAccumDepreciation) => {
    form.value.netValue = form.value.originalValueWithTax - Number(newAccumDepreciation || 0)
  }
)

// 监听currentAsset的变化
watch(() => props.currentAsset, (newAsset) => {
  if (newAsset && open.value) {
    nextTick(() => {
      initForm()
    })
  }
}, { deep: true })

// 初始化表单数据
const initForm = () => {
  form.value = {
    id: props.currentAsset.id,
    assetNumber: props.currentAsset.assetNumber || '',
    assetName: props.currentAsset.assetName || '',
    specification: props.currentAsset.specification || '',
    originalValue: props.currentAsset.originalValue || 0,
    taxAmount: props.currentAsset.taxAmount || 0,
    originalValueWithTax: (props.currentAsset.originalValue || 0) + (props.currentAsset.taxAmount || 0),
    accumDepreciation: props.currentAsset.accumDepreciation || 0,
    netValue: props.currentAsset.netValue || 0,
    monthlyDepreciation: props.currentAsset.monthlyDepreciation || 0,
    depreciationMethod: props.currentAsset.depreciationMethod || '',
    estimatedLife: props.currentAsset.estimatedLife || 0,
    usedYear: props.currentAsset.usedYear || '',
    assetCategory: props.currentAsset.assetCategory || '',
    purchaseDate: props.currentAsset.purchaseDate || '',
    entryPeriod: props.currentAsset.entryPeriod || '',
    startUseDate: props.currentAsset.startUseDate || '',
    location: props.currentAsset.location || '',
    useDepartment: props.currentAsset.useDepartment ? data.departmentCodeMap[props.currentAsset.useDepartment] || props.currentAsset.useDepartment : '',
    user: props.currentAsset.user || '',
    supplier: props.currentAsset.supplier || '',
    addType: props.currentAsset.addType || '',
    copyCount: props.currentAsset.copyCount || 0,
    depreciateNow: props.currentAsset.depreciateNow || 0,
    residualRate: props.currentAsset.residualRate || 0,
    impairmentAccount: props.currentAsset.impairmentAccount ,
    fixedAssetAccount: props.currentAsset.fixedAssetAccount ,
    accumDepreciationAccount: props.currentAsset.accumDepreciationAccount ,
    depreciationExpenseAccount: props.currentAsset.depreciationExpenseAccount ,
    taxAccount: props.currentAsset.taxAccount ,
    assetDisposalAccount: props.currentAsset.assetDisposalAccount,
    purchaseAccount: props.currentAsset.purchaseAccount ,
    changeDate: '',
    remark: ''
  }
}

// 打开弹窗
const show = () => {
  open.value = true
  nextTick(() => {
    initForm()
  })
}

// 关闭弹窗
const cancel = () => {
  open.value = false
  reset()
}

// 重置表单
const reset = () => {
  if (changeFormRef.value) {
    changeFormRef.value.resetFields()
  }
  if (lifeFormRef.value) {
    lifeFormRef.value.resetFields()
  }
  if (methodFormRef.value) {
    methodFormRef.value.resetFields()
  }
  if (accumDepreciationFormRef.value) {
    accumDepreciationFormRef.value.resetFields()
  }
  if (residualFormRef.value) {
    residualFormRef.value.resetFields()
  }
  if (deptFormRef.value) {
    deptFormRef.value.resetFields()
  }
  if (accountFormRef.value) {
    accountFormRef.value.resetFields()
  }
  form.value = {
    id: null,
    assetNumber: '',
    assetName: '',
    specification: '',
    originalValue: 0,
    taxAmount: 0,
    originalValueWithTax: 0,
    accumDepreciation: 0,
    netValue: 0,
    monthlyDepreciation: 0,
    depreciationMethod: '',
    estimatedLife: 0,
    usedYear: '',
    assetCategory: '',
    purchaseDate: '',
    entryPeriod: '',
    startUseDate: '',
    location: '',
    useDepartment: '',
    user: '',
    supplier: '',
    addType: '',
    copyCount: 0,
    depreciateNow: 0,
    residualRate: 0,
    impairmentAccount: '',
    fixedAssetAccount: '',
    accumDepreciationAccount: '',
    depreciationExpenseAccount: '',
    taxAccount: '',
    assetDisposalAccount: '',
    purchaseAccount: '',
    changeDate: '',
    remark: ''
  }
  lifeForm.value = {
    estimatedLife: 0,
    changeDate: '',
    remark: ''
  }
  methodForm.value = {
    depreciationMethod: '',
    residualRate: 0,
    estimatedLife: 0,
    changeDate: '',
    remark: ''
  }
  accumDepreciationForm.value = {
    accumDepreciation: 0,
    changeDate: '',
    remark: ''
  }
  residualForm.value = {
    residualRate: 0,
    changeDate: '',
    remark: ''
  }
  deptForm.value = {
    useDepartment: '',
    user: '',
    changeDate: '',
    remark: ''
  }
  accountForm.value = {
    fixedAssetAccount: '',
    depreciationAccount: '',
    accumulatedDepreciationAccount: '',
    depreciationExpenseAccount: '',
    taxAccount: '',
    assetClearAccount: '',
    assetPurchaseAccount: '',
    changeDate: '',
    remark: ''
  }
}

// 提交表单
const submitForm = () => {
  const formRef = changeFormRef.value
  
  formRef?.validate((valid) => {
    if (valid) {
      const submitData = {
        ...form.value,
         monthlyDepreciation:calculatedResidualMonthlyDepreciation.value,
        assetId: props.currentAsset.id,
        type: activeTab.value
      }
      emit('success', submitData)
      cancel()
    }
  })
}

// 暴露方法给父组件
defineExpose({
  show
})
</script>

<style scoped>
.asset-info {
  margin-bottom: 20px;
}

.asset-tabs {
  margin-top: 20px;
}

.comparison-container {
  display: flex;
  gap: 40px;
}

.current-info,
.change-info {
  flex: 1;
}

.current-info h3,
.change-info h3 {
  margin-bottom: 20px;
  font-weight: 500;
  color: #303133;
  border-bottom: 1px solid #EBEEF5;
  padding-bottom: 10px;
}

.info-form .el-form-item {
  margin-bottom: 18px;
}

.under-development {
  text-align: center;
  color: #909399;
  padding: 40px 0;
}

:deep(.el-descriptions__label) {
  width: 120px;
  justify-content: flex-end;
}

.unit {
  margin-left: 8px;
  color: #606266;
}
</style> 